#import machine from machine import I2C, Pin import utime # Define MCP4725 constants MCP4725_I2C_ADDR = 0x60 DAC_REGISTER = 0x40 # Raspberry Pi Pico constants SDA_PIN = 8 #physical pin 11 SCL_PIN = 9 #physical pin 12 ########################################################################## def scan_i2c(): i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=100000) # Adjust pins and frequency as needed devices = i2c.scan() if devices: print("I2C devices found:") for device in devices: print(hex(device)) else: print("No I2C devices found.") scan_i2c() ######################################################################### # Initialize I2C i2c = I2C(0, sda=Pin(SDA_PIN), scl=Pin(SCL_PIN), freq=100000) # Set up I2C #i2c = machine.I2C(0) # Use I2C 0 #1i2c.init() # Function to write a value to the MCP4725 DAC def write_dac(value): # Calculate the DAC data value dac_value = int(value * 4095 / 3.3) # Convert voltage to 12-bit DAC value # Construct the data packet data = bytearray([(dac_value >> 8) & 0xFF, dac_value & 0xFF]) # Write the data to the MCP4725 i2c.writeto(MCP4725_I2C_ADDR, data) # Main loop while True: try: # Prompt user for voltage value voltage = float(input("Enter voltage (0.0 to 3.3 V): ")) # Write the voltage to the MCP4725 DAC write_dac(voltage) print("Voltage set to {:.2f} V".format(voltage)) except ValueError: print("Invalid input. Please enter a numeric value.") except KeyboardInterrupt: print("\nProgram terminated by user.") break utime.sleep(1)